<template>
  <div @click="showDeptList = false">
    <el-form>
      <el-row>
        <el-col :span="6">
          <el-form-item label-width="40px" label="月份">
            <el-select
              v-model="searchForm.reportDate"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in versionList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="80px" label="片区/工厂">
            <div class="tree-box">
              <div class="input-wrap">
                <el-input
                  v-model.trim="filterText"
                  class="input-box"
                  placeholder="请选择"
                  clearable
                  @clear="clearTreeSearch"
                  @click.stop="showDeptList = true"
                ></el-input>
              </div>
              <el-tree
                v-show="showDeptList"
                ref="treeRef"
                class="filter-tree"
                :data="deptList"
                :props="defaultProps"
                default-expand-all
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                @node-click="handleChangeDept"
              />
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="80px" label="项目名称">
            <el-input
              clearable
              placeholder="请选择"
              v-model="searchForm.projectNameLike"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" class="search-btn">
          <el-button type="primary" @click="handleSearch">
            <el-icon class="icon-plus"><Search /></el-icon>查询
          </el-button>
          <el-button type="primary" @click="handleReset">重置 </el-button>
        </el-col>
      </el-row>
    </el-form>
    <div style="margin-bottom: 10px">
      <el-button @click="handleExport">导出 </el-button>
    </div>
    <el-skeleton
      style="height: calc(100vh - 340px)"
      :rows="8"
      :loading="loading"
      animated
    >
    </el-skeleton>
    <el-table
      :data="tableData"
      style="width: 100%"
      v-loading="loading"
      v-show="!loading"
      :height="'calc(100vh - 340px)'"
    >
      <el-table-column label="项目审批" align="center">
        <el-table-column align="center" type="index" label="序号" width="50">
          <template v-slot="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="projectTypeName" label="项目类别" width="80" />
        <el-table-column
          prop="projectRegionName"
          label="总部/片区/事业部"
          width="140"
        />
        <el-table-column
          prop="projectFactoryName"
          label="总部部门/工厂/大区"
          width="160"
        />
        <el-table-column prop="projectCode" label="项目编号" width="200" />
        <el-table-column prop="projectName" label="项目名称" width="200" />
        <el-table-column
          prop="projectNatureName"
          label="项目性质"
          width="120"
        />
        <el-table-column
          prop="expensesTypeName"
          label="费用专项类别"
          width="120"
        />
        <el-table-column
          prop="investmentTypeName"
          label="投资方向"
          width="120"
        />
        <el-table-column
          prop="declareType"
          label="集中申报/临时申报"
          width="160"
        >
          <template v-slot="scope">
            <span>{{
              scope.row.declareType == 1
                ? "集中申报"
                : scope.row.declareType == 0
                ? "临时申报"
                : "-"
            }}</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="approvalAmount"
          label="立项批准金额（万元）"
          width="180"
        />
        <el-table-column prop="approvalTime" label="立项批准时间" width="160" />
        <el-table-column
          prop="transferAmount"
          label="其中调拨资产金额（万元）"
          width="200"
        >
          <template #default="{ row }">
            <span>{{
              row.transferAmount || row.transferAmount === 0
                ? row.transferAmount
                : "-"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="approvalSubTransferAmount"
          label="占用当年额度金额"
          width="160"
        >
          <template #default="{ row }">
            <span>{{
              row.approvalSubTransferAmount ||
              row.approvalSubTransferAmount === 0
                ? row.approvalSubTransferAmount
                : "-"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="projectProgressName"
          label="项目实施进度"
          width="160"
        />
      </el-table-column>
      <el-table-column label="合同管理" align="center">
        <el-table-column
          prop="contractList"
          label="合同编号"
          width="120"
          class-name="cus-column"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              :columnKey="'contractNum'"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="已签订合同名称"
          width="200"
          prop="contractList"
          class-name="cus-column"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              :columnKey="'contractName'"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="合同金额（不含税，元)"
          width="160"
          prop="contractList"
          class-name="cus-column"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              :columnKey="'currencyAmountNoTax'"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          label="合同金额（含税，元)"
          width="140"
          prop="contractList"
          class-name="cus-column"
          ><template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              :columnKey="'contractAmount'"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          label="供应商/承包商 "
          width="200"
          prop="contractList"
          class-name="cus-column"
          ><template v-slot="scope">
            <PubColumn :data="scope.row.contractList" :columnKey="'supplier'" />
          </template>
        </el-table-column>
        <el-table-column
          label="合同签订日期"
          width="120"
          prop="contractList"
          class-name="cus-column"
          ><template v-slot="scope">
            <PubColumn :data="scope.row.contractList" :columnKey="'signDate'" />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="合同实施进度" align="center">
        <el-table-column
          label="目前执行情况"
          width="120"
          prop="contractList"
          class-name="cus-column"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="contractProgressName"
            />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="付款管理" align="center">
        <el-table-column
          label="付款时间"
          width="180"
          prop="contractList"
          class-name="cus-column"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentTime"
              childKey="paymentList"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="付款金额（元）"
          width="180"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentAmount"
              childKey="paymentList"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="付款金额合计（元）"
          width="180"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentTotalAmount"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="付款比例"
          width="120"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentTotalRate"
            />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="预转固管理" align="center">
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="预转固时间"
          width="160"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentTime"
              childKey="preTransferList"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="预转固金额(元）"
          width="180"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentAmount"
              childKey="preTransferList"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="预转固金额合计(元）"
          width="180"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="preTransferTotalAmount"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="预转固比例"
          width="120"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="preTransferTotalRate"
            />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="正式转固管理" align="center">
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="正式转固时间"
          width="160"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentTime"
              childKey="formalTransferList"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="正式转固金额(元)"
          width="180"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="paymentAmount"
              childKey="formalTransferList"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="正式转固金额合计(元)"
          width="180"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="formalTransferTotalAmount"
              isFormat
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="contractList"
          class-name="cus-column"
          label="正式转固比例"
          width="120"
        >
          <template v-slot="scope">
            <PubColumn
              :data="scope.row.contractList"
              columnKey="formalTransferTotalRate"
            />
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>
    <re-pagination
      v-if="pageInfo.total > 0"
      :total="pageInfo.total"
      v-model:page="pageInfo.pageNum"
      v-model:limit="pageInfo.pageSize"
      @pagination="handleCurrentChange(pageInfo.pageNum)"
    />
  </div>
</template>

<script setup lang="ts">
import PubColumn from "./_help";
import { getReportDate, getList } from "/@/api/reportManager";
import { getDeptTreeByPro } from "/@/api/projects";
import { ref, onMounted, watchEffect, watch } from "vue";
import { exportExcel } from "/@/utils/file";
import { ElTree } from "element-plus";
import RePagination from "/@/components/RePagination/index.vue";

const searchForm = ref({
  projectNameLike: "",
  projectFactoryCode: "",
  reportDate: ""
});

const defaultProps = {
  children: "children",
  label: "deptDescShrt",
  disabled: "false"
};

const props = defineProps({
  activeYear: {
    type: String,
    default: ""
  },
  activeName: {
    type: String,
    default: ""
  }
});

watchEffect(() => {
  if (props.activeName === "first" && props.activeYear) {
    queryReportDate();
  }
});

const showDeptList = ref(false);
const loading = ref<boolean>(false);

const filterText = ref("");
const treeRef = ref<InstanceType<typeof ElTree>>();
watch(filterText, val => {
  treeRef.value!.filter(val);
});

onMounted(() => {
  getDeptTreeFunc();
});

// 清空片区
const clearTreeSearch = () => {
  filterText.value = "";
  searchForm.value.projectFactoryCode = "";
  showDeptList.value = false;
};

// 选中片区
const handleChangeDept = node => {
  filterText.value = node.deptDescShrt;
  searchForm.value.projectFactoryCode = node.id;
  showDeptList.value = false;
};

// 过滤片区
const filterNode = (value: string, data) => {
  if (!value) return true;
  return data.deptDescShrt.includes(value);
};

// 获取工厂区域
const deptList = ref([]);
const getDeptTreeFunc = async () => {
  try {
    const { data } = await getDeptTreeByPro({});
    deptList.value = data || [];
  } catch (err) {
    console.log(err);
  }
};

// 获取版本
const versionList = ref([]);
const queryReportDate = async () => {
  const { data } = await getReportDate({ projectYear: props.activeYear });
  versionList.value = data || [];
  searchForm.value.reportDate = data[0];
  queryList();
};

// 搜索
const handleSearch = () => {
  queryList();
};

// 重置
const handleReset = () => {
  searchForm.value = {
    projectNameLike: "",
    projectFactoryCode: "",
    reportDate: versionList.value[0]
  };
  filterText.value = "";
};

// 分页
const handleCurrentChange = (val: number) => {
  pageInfo.value.pageNum = val;
  queryList();
};

const pageInfo = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  pageOpt: [10, 20, 30, 50]
});
const tableData = ref([]);
const queryList = async () => {
  const params = {
    pageNum: pageInfo.value.pageNum,
    pageSize: pageInfo.value.pageSize,
    projectYear: props.activeYear,
    ...searchForm.value
  };
  loading.value = true;
  const { data } = await getList(params);
  const dataList = data.list.map(item => {
    if (item.contractList && item.contractList.length) {
      item.contractList.forEach(i => {
        i.maxNumber = Math.max(
          i.paymentList.length,
          i.formalTransferList.length,
          i.preTransferList.length
        );
      });
    }
    return item;
  });
  tableData.value = dataList || [];
  pageInfo.value.total = data.rows || 0;
  loading.value = false;
};

// 导出数据
const handleExport = () => {
  const name = `B类项目月报表`;
  const data = {
    projectYear: props.activeYear,
    ...searchForm.value
  };
  exportExcel("/snow-admin//thdReportMonth/exportMonthReport", data, name);
};
</script>
<style scoped lang="scss">
.tree-box {
  position: relative;
  width: 100%;
  .input-wrap {
    display: flex;
    align-items: center;
    width: 100%;
    padding-left: 8px;
    padding-right: 6px;
  }
  &:deep {
    .input-box {
      .el-input__inner {
        height: 32px;
        line-height: 32px;
        font-size: 12px;
        border: 1px solid #e4e8f2;
        padding: 4px 8px;
      }
    }
  }
}
.filter-tree:deep {
  &.el-tree {
    width: 100%;
    max-height: 552px;
    overflow-x: auto;
    overflow-y: auto;
    border: 1px solid #e4e8f2;
    border-radius: 8px;
    z-index: 10;
    font-weight: normal;
    font-size: 12px;
    position: absolute;
    .el-tree-node > .el-tree-node__children {
      overflow: visible;
    }
  }
}

::v-deep .cus-column {
  padding: 0 !important;
  .cell {
    padding: 0 !important;
  }

  .contract-list-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .contract-list {
    width: 100%;
    padding: 6px;
    padding: 6px;
    border-bottom: 1px solid #e4e8f2;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .no-border {
    border-bottom: none;
  }
}
</style>
